using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Application;
using Decav.Gatsb.Data;
using Decav.Gatsb;
using Decav.Gatsb.Web;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((IMasterPageTemplate)Master).SelectedPage = MainMenuNavigationButton.Search;

        if (IsPostBack)
            return;

        NoSearchResultsFound.Visible = false;
        if (SearchQuery != null && (SearchQuery == searchTextBoxBox.Text || searchTextBoxBox.Text == string.Empty))
        {
            searchTextBoxBox.Text = SearchQuery;
            RunSearch();

            if (!SearchResultsPlaces.Visible && !SearchResultsPeople.Visible)
                NoSearchResultsFound.Visible = true;
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(typeof(Search), "Focus_SearchBox",
                string.Format("document.getElementById(\"{0}\").focus();", searchTextBoxBox.ClientID),
                true);
        }
    }

    protected virtual void Search_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/Search.aspx?q={0}", Server.UrlEncode(searchTextBoxBox.Text)), true);
    }

    private void RunSearch()
    {
        RunPeopleQuery();
        RunPlacesQuery();
    }

    private void RunPeopleQuery()
    {
        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
        {
            Decav.Gatsb.User[] users = null;
            
            if (User.Identity.IsAuthenticated)
                users = adapt.FindPeople(new Decav.Gatsb.PersonSearchCriteria(SearchQuery));

            if (users == null || users.Length == 0)
            {
                // Because we're searching something else too,
                // just suppress this result set.
                if (SearchPlaces)
                    SearchResultsPeople.Visible = false;
                else
                    noPeopleFoundMessage.Visible = true;

                peopleResultsList.Visible = false;
            }
            else
            {
                SearchResultsPeople.Visible = true;
                noPeopleFoundMessage.Visible = false;
                peopleResultsList.Visible = true;

                peopleResultsList.DataSource = users;
                peopleResultsList.DataBind();
            }
        }
    }

    private void RunPlacesQuery()
    {
        Location[] locations = GatsbApplication.Current.FindLocation(LocationSearchCriteria.Parse(SearchQuery));

        if (locations.Length == 0)
        {
            // Because we're searching something else too,
            // just suppress this result set.
            if (SearchPeople)
                SearchResultsPlaces.Visible = false;
            else
                noPlacesFoundMessage.Visible = true;
    
            placesResultsList.Visible = false;
        }
        else
        {
            SearchResultsPlaces.Visible = true;
            noPlacesFoundMessage.Visible = false;
            placesResultsList.Visible = true;

            placesResultsList.DataSource = locations;
            placesResultsList.DataBind();
        }
    }

    /// <summary>
    /// Gets the search query to run.
    /// </summary>
    public string SearchQuery
    {
        get
        {
            if (Request.QueryString["q"] == null || Request.QueryString["q"].Trim() == string.Empty)
                return null;

            return Request.QueryString["q"];
        }
    }

    /// <summary>
    /// Gets whether or not places should be searched
    /// </summary>
    public bool SearchPlaces
    {
        get
        {
            if (Request.QueryString["places"] != null && Request.QueryString["places"].Trim().Length > 0)
                return bool.Parse(Request.QueryString["places"]);

            return true;
        }
    }

    /// <summary>
    /// Gets whether or not people should be searched
    /// </summary>
    public bool SearchPeople
    {
        get
        {
            if (Request.QueryString["people"] != null && Request.QueryString["people"].Trim().Length > 0)
                return bool.Parse(Request.QueryString["people"]);

            return true;
        }
    }
}
